<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:job="livcos.org/ns/process/job"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:u="livcos.org/ns/cosmos/update"
  xmlns:m="livcos.org/ns/mind"
  exclude-result-prefixes="c job u m">
  
  <xsl:import href="../../web/editor/Edit.xsl"/>
  
  <xsl:param name="thgt"/>
  <xsl:param name="label"/>
  <xsl:param name="rel"/>
  <xsl:param name="resp"/>
  <xsl:param name="objId"/>
  
  <xsl:variable name="thought" select="document(concat($thgt,'.content'))/*"/>

  <xsl:template match="/">
    <xsl:apply-templates select="*/c:request/parameter/*" />
    <job:feed stream="response" base="job" ref="/livcos.org/MindNavi/web/thought" content-type="text/plain">
      <job:param name="ref" value="{$resp}"/>
    </job:feed>
  </xsl:template>
  
  <xsl:template match="*">
  </xsl:template>
  
  <xsl:template match="update">
    <u:context ref="{$objId}" scope="content">
      <xsl:apply-imports/>
    </u:context>
  </xsl:template>
  
  <xsl:template match="create">
    <xsl:variable name="setObjId" select="replace($thgt, '/[^/]+$', '')"/>
    <xsl:variable name="objPath">
      <xsl:for-each select="tokenize($label, '/')">
        <xsl:if test="position() > 1">/</xsl:if>
        <xsl:variable name="l" select="translate(encode-for-uri(translate(., ' -.~', '____')), '%', '_')"/>
        <xsl:value-of select="if (matches($l,'^[0-9]')) then concat('_',$l) else $l"/>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="objId" select="concat($setObjId, '/', $objPath)"/>
    <xsl:if test="$objId != $thgt">
      <xsl:choose>
        <xsl:when test="doc-available(concat($objId,'.content'))">
          <xsl:variable name="newThought" select="document(concat($objId,'.content'))/*"/>
          <xsl:if test="not($newThought/m:rel[@ref = $thgt])">
            <u:context ref="{$objId}" scope="content">
              <u:insert select=".">
                <rel id="{{new-id()}}" ref="{$thgt}" xmlns="livcos.org/ns/mind"/>
              </u:insert>
            </u:context>
          </xsl:if>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="testAndCreatePath">
            <xsl:with-param name="base" select="$setObjId"/>
            <xsl:with-param name="path" select="tokenize($objPath, '/')"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="not($thought/m:rel[@ref = $objId])">
        <u:context ref="{$thgt}" scope="content">
          <u:insert select=".">
            <rel id="{{new-id()}}" ref="{$objId}" xmlns="livcos.org/ns/mind"/>
          </u:insert>
        </u:context>
      </xsl:if>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="testAndCreatePath">
    <xsl:param name="base"/>
    <xsl:param name="path"/>
    <xsl:choose>
      <xsl:when test="doc-available(concat($base, '/', $path[1],'.meta'))">
        <xsl:call-template name="testAndCreatePath">
          <xsl:with-param name="base" select="concat($base, '/', $path[1])"/>
          <xsl:with-param name="path" select="subsequence($path, 2)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <u:context ref="{$base}" scope="meta">
          <u:insert select="da:folder[1]">
            <u:namespace prefix="da" uri="livcos.org/ns/cosmos/data-access"/>
            <xsl:call-template name="createPath">
              <xsl:with-param name="path" select="$path"/>
            </xsl:call-template>
          </u:insert>
        </u:context>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="createPath">
    <xsl:param name="path"/>
    <xsl:element name="{$path[1]}" namespace="livcos.org/ns/cosmos">
      <xsl:choose>
        <xsl:when test="count($path) &lt; 2">
          <thought xmlns="livcos.org/ns/mind">
            <label><xsl:value-of select="tokenize($label, '/')[last()]"/></label>
            <rel id="rel_1" ref="{$thgt}"/>
          </thought>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="createPath">
            <xsl:with-param name="path" select="subsequence($path, 2)"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="delete">
    <u:context ref="{$thgt}" scope="content">
      <u:delete select="*[@id = '{$rel}']"/>
    </u:context>
  </xsl:template>
  
</xsl:stylesheet>
